package top.lenjon.lib.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.lenjon.lib.model.ResponseModel;
import top.lenjon.lib.service.ILogService;

import java.util.Optional;

/**
 * @Author whz
 * @Date 2020/11/9 16:34
 **/
@RestController
@RequestMapping("/manager")
public class BookLogController {
    public final int ALL_LOG=2;
    public final int NO_LOG=1;
    public final int HS_LOG=0;

    @Autowired
    ILogService iLogService;

    /**
     * description:
     * 所有借阅记录
     * @param
     * @return top.lenjon.lib.model.ResponseModel
     */
    @GetMapping("/allLog")
    public ResponseModel getLog(@RequestParam(defaultValue = "1") int pageNum,
                                @RequestParam(defaultValue = "10")int pageSize){
        return Optional.ofNullable(iLogService.getLogByStatus(ALL_LOG,pageNum,pageSize))
                .map(u->ResponseModel.success("s",u))
                .orElse(ResponseModel.error("fail"));
    }

    /**
     * description:
     * 未归还记录
     * @param
     * @return top.lenjon.lib.model.ResponseModel
     */
    @GetMapping("/noReturnLog")
    public ResponseModel getNoLog(@RequestParam(defaultValue = "1") int pageNum,
                                  @RequestParam(defaultValue = "10")int pageSize){
        return Optional.ofNullable(iLogService.getLogByStatus(NO_LOG,pageNum,pageSize))
                .map(u->ResponseModel.success("s",u))
                .orElse(ResponseModel.error("fail"));
    }

    /**
     * description:
     * 已归还记录
     * @param
     * @return top.lenjon.lib.model.ResponseModel
     */
    @GetMapping("/hasReturnLog")
    public ResponseModel getHsLog(@RequestParam(defaultValue = "1") int pageNum,
                                  @RequestParam(defaultValue = "10")int pageSize){
        return Optional.ofNullable(iLogService.getLogByStatus(HS_LOG,pageNum,pageSize))
                .map(u->ResponseModel.success("s",u))
                .orElse(ResponseModel.error("fail"));
    }

    /**
     * description:
     * 记录细节
     * @param id
     * @return top.lenjon.lib.model.ResponseModel
     */
    @GetMapping("/detail")
    public ResponseModel detail(int id){
        return Optional.ofNullable(iLogService.getLogById(id))
                .map(u->ResponseModel.success("s",u))
                .orElse(ResponseModel.error("fail"));    }
}
